<?php
/**
 * @package		简易CMS
 * @subpackage	文章自定义管理模块
 * @version		$Id: admincp_customfields.php 27 2012-09-23 17:39:09Z htmambo@gmail.com $
 * @author		Hoping
 * @copyright	Copyright (C) 2010 Hoping Software Studio.
 */


if (!defined('IN_JYCMS') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}
cpheader();
shownav('portal', 'Customfields');
$operation = in_array($operation, array('add', 'list', 'edit')) ? $operation : 'list';
$theurl = 'action=customfields';
$listarr = array();
$thevalue = array();

//[POST]
if (submitcheck('listsubmit')) {
	//LIST UPDATE
	if (!empty($_POST['displayorder'])) {
		if (is_array($_POST['displayorder'])) {
			foreach ($_POST['displayorder'] as $customfieldkey => $customfielddisplayordervalue) {
				$wheresqlarr = array('customfieldid' => $customfieldkey, 'uid' => $_G['uid']);
				if (!empty($_POST['delete'][$customfieldkey])) {
					DB::delete('common_customfields', $wheresqlarr);
				} else {
					$setsqlarr = array('displayorder' => intval($_POST['displayorder'][$customfieldkey]));
					DB::update('common_customfields', $setsqlarr, $wheresqlarr);
				}
			}
		}
	}
	cpmsg('customfield_list_update_success', $theurl);
} elseif (submitcheck('valuesubmit')) {
	if (strlen($_G['gp_name']) < 1 || strlen($_G['gp_name']) > 50) {
		$_POST['name'] = $_G['timestamp'];
	}
	//ONE SUBMIT
	$cfarr = array();
	if (is_array($_G['gp_customfieldname'])) {
		$o = $_G['gp_customfieldorder'];
		asort($o);
		foreach ($o as $ckey => $order) {
			$cname = $_G['gp_customfieldname'][$ckey];
			$cname = trim($cname);
			if ($cname) {
				$ctype = $_G['gp_customfieldtype'][$ckey];
				$coption = $_G['gp_customfieldoption'][$ckey];
				$position = $_G['gp_customfieldposition'][$ckey];
				$order = $_G['gp_customfieldorder'][$ckey];
				$cfarr[] = array('type' => $ctype, 'name' => $cname, 'option' => $coption, 'position' => $position, 'order' => $order);
			}
		}
	}
	$cfstr = addslashes(serialize($cfarr));
	if (empty($_G['gp_customfieldid'])) {
		//ADD
		$insertsqlarr = array(
			'uid' => $_G['uid'],
			'name' => $_G['gp_name'],
			'displayorder' => intval($_G['gp_displayorder']),
			'customfieldtext' => $cfstr,
			'isdefault' => $_G['gpisdefault']
		);
		DB::insert('common_customfields', $insertsqlarr);
		$msg = '自定义信息已经添加成功！';
	} else {
		//UPDATE
		$setsqlarr = array(
			'name' => $_POST['name'],
			'displayorder' => intval($_POST['displayorder']),
			'customfieldtext' => $cfstr,
			'isdefault' => $_POST['isdefault']
		);
		$wheresqlarr = array(
			'customfieldid' => $_POST['customfieldid'],
			'uid' => $_G['uid']
		);
		DB::update('common_customfields', $setsqlarr, $wheresqlarr);
		$msg = '自定义信息已经成功更新！';
	}
	updatecache('customfields');
	cpmsg($msg, $theurl);
} else {
	showsubmenu('Customfields', array(
		array('List', 'customfields', $operation == 'list'),
		array('Add', 'customfields&operation=add', $operation == 'add')
			), $searchctrl);
	if ($operation=='list') {
		$wheresqlarr = array();
		$plussql = 'ORDER BY displayorder';
		$query = DB::query("SELECT * FROM #__common_customfields ORDER BY displayorder");
		$listarr = array();
		while($value = DB::fetch($query)){
			$cfarr = unserialize($value['customfieldtext']);
			$cfnamearr = array();
			if($cfarr)
			foreach ($cfarr as $cfvalue) {
				$cfnamearr[] = $cfvalue['name'];
			}
			$cftext = implode(', ', $cfnamearr);
			$value['fields'] = $cftext;
			if($value['isdefault']) {
				$value['name'] = '<b>'.$value['name'].'</b>';
				$value['isdefault'] = '是';
			} else {
				$value['isdefault'] = '否';
			}
			$listarr[] = $value;
		}
	} elseif ($operation == 'edit') {
		$customfieldid = intval($_G['gp_customfieldid']);
		if(!$customfieldid){
			cpmsg('对不起，您没有选择要编辑的自定义信息！');
		}
		$wheresqlarr = array(
			'customfieldid' => $customfieldid
		);
		$thevalues = DB::select('common_customfields', '*', $wheresqlarr);
		if (empty($thevalues[0])) {
			cpmsg('对不起，没有找到指定的自定义信息！');
		} else {
			$thevalue = $thevalues[0];
		}
		if(is_array($thevalue)){
			$thevalue['customfieldtext'] = unserialize($thevalue['customfieldtext']);
		}
	} elseif ($operation == 'add') {
		$thevalue = array(
			'customfieldid' => '0',
			'name' => '',
			'displayorder' => '0',
			'customfieldtext' => serialize(array(array('type' => 'input', 'name' => '', 'option' => '', 'position' => '1', 'order' => '0'))),
			'isdefault' => 0
		);
	}
}
if($operation == 'list'){
	showtips('<li>通过自定义信息，您可以实现在添加信息时为自己的信息填写更多属性，从而可以让信息共同拥有的某些属性以统一的样式展示给浏览者。</li><li>通过<u>添加新配置</u>操作，您就可以进行信息个性字段的设置了。</li>');
} else {
	$cftypearr = array(
		'input' => '输入框',
		'select' => '选择框',
		'textarea' => '多行文本',
		'url' => '网址',
		'urls' => '网址列表'
	);
	$jsstr = '<table><tr valign=\"top\"><td width=\"130\"><input type=\"text\" name=\"customfieldname[]\" id=\"aaa\" size=\"20\" value=\"\" /></td>';
	$jsstr .= '<td width=\"135\"><select name=\"customfieldtype[]\" style=\"width: 98%;\">';
	foreach((array)$cftypearr as $k=>$v){
		$jsstr .= '<option value=\"'.$k.'\">'.$v.'</option>';
	}
	$jsstr .= '</select></td><td width=\"225\">';
	$jsstr .= '<textarea name=\"customfieldoption[]\" rows=\"5\" style=\"width: 98%;\"></textarea></td><td width=\"135\">';
	$jsstr .= '<select name=\"customfieldposition[]\" style=\"width: 98%;\" /><option value=\"0\" selected>正文顶部</option><option value=\"1\">正文底部</option></select></td>';
	$jsstr .= '<td><input type=\"text\" name=\"customfieldorder[]\"></td>';
	$jsstr .= '</tr></table>';
	showtips('<li>您可以通过点击<u>添加新字段</u>按钮，为该自定义信息设置多个不同的字段。</li><li>每一个字段的类型，可以设置为单行的文本输入框、多行的文本区域、单选的列表框、多选的复选框等。</li><li>当指定字段类型为列表框、复选框时，您需要在列表选项输入预先的选项，每行为一个选项。</li>');
}
include template('admin/customfields');
